package com.rxyl.love.petdoctor.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by dd on 2016/1/12 0012.
 */
public class SideView extends View {

    private static String[] labels = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private Paint paint, checkPaint;
    private int txtHeight;
    private int checkedIndex = -1;
    private OnSelectListener onSelectListener;

    public SideView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.parseColor("#888888"));
        paint.setTextSize(30);
        paint.setAntiAlias(true);

        checkPaint = new Paint();
        checkPaint.setColor(Color.parseColor("#1D99F6"));
        checkPaint.setTextSize(30);
        checkPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        txtHeight = getHeight() / labels.length;
        for (int i = 0; i < labels.length; i++) {
            if (i == checkedIndex) {
                canvas.drawText(labels[i], getWidth() / 2 - paint.measureText(labels[i]) / 2, txtHeight * (i + 1), checkPaint);
            } else {
                canvas.drawText(labels[i], getWidth() / 2 - paint.measureText(labels[i]) / 2, txtHeight * (i + 1), paint);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWH(widthMeasureSpec, 0), measureWH(heightMeasureSpec, 1));
    }

    private int measureWH(int measureSpec, int type) {
        int model = MeasureSpec.getMode(measureSpec);//获得当前空间值的一个模式
        int size = MeasureSpec.getSize(measureSpec);//获得当前空间值的推荐值

        switch (model) {
            case MeasureSpec.EXACTLY:
                return size;
            case MeasureSpec.AT_MOST:
                if (type == 0) {
                    //测量宽度
                    size = (int) paint.measureText(labels[0]);
                    return size;
                } else {
                    //测量高度
                    return size;
                }
            case MeasureSpec.UNSPECIFIED:
                break;
        }
        return 0;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                moveY(y);
                break;
            case MotionEvent.ACTION_MOVE:
                moveY(y);
                break;
            case MotionEvent.ACTION_UP:
                checkedIndex = -1;
                invalidate();
                break;
        }
        return true;
    }

    private void moveY(int y) {
        int checkindex = y / txtHeight;
        if (checkindex < 0) {
            checkindex = 0;
        }

        if (checkindex >= labels.length) {
            checkindex = labels.length - 1;
        }

        //如果当前选中的下标和上次记录的下标一样，则不再进行重绘，避免不必要的性能损耗
        if (checkindex == checkedIndex) {
            return;
        }

        checkedIndex = checkindex;

        if (onSelectListener != null) {
            onSelectListener.onSelect(labels[checkedIndex]);
        }

        invalidate();//重绘控件 -- 调用该方法后会立刻执行onDraw方法
    }

    public interface OnSelectListener {
        public void onSelect(String str);
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }
}
